//递归转迭代
function reduce(
  a,
  b,
  next = x => x + 1,
  term = x => x,
  accumulator = 0,
  combiner = (x, y) => x + y
) {
  if (a > b) return accumulator;
  return reduce(next(a), b, next, term, combiner(term(a), accumulator));
}

// compose2
function compose2(...fns) {
  fns.forEach(fn => console.log(fn.name));
  return fns.reduce((a, b) => (...args) => a(b(...args)));                    
}
//f(g())
const f = x => {
  console.log('f', x);
  return x + 1;
};
const g = x => {
  console.log('g', x);
  return x * 2;
};
const h = x => {
  console.log('h', x);
  return x ** 3;
};
console.log(compose2(f, g, h)(2));
